package com.tchy.cloud.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tchy.cloud.entity.*;
import com.tchy.cloud.service.CQuestionnaireService;
import jodd.bean.BeanUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.ComboTree;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.TreeGrid;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.tag.vo.easyui.ComboTreeModel;
import org.jeecgframework.tag.vo.easyui.TreeGridModel;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;


/**
 * 量表
 * xmweilove
 */
@Controller
@RequestMapping("/questController")
public class CQuestionnaireController extends BaseController {
    private static final Logger logger = Logger.getLogger(CQuestionnaireController.class);

    @Autowired
    private CommonService commonService;

    /**
     * 量表展示页面跳转
     *
     * @return
     */
    @RequestMapping(params = "index")
    public ModelAndView index() {
        return new ModelAndView("clound/quest/index");
    }

    /**
     * 量表展示数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(CQuestionnaireEntity questionnaireEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(CQuestionnaireEntity.class, dataGrid);

        cq.eq("deleted", 0);
        cq.addOrder("id", SortDirection.desc);

        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, questionnaireEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }


    /**
     * 量表展示数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "select_datagrid")
    public void select_datagrid(CQuestionnaireEntity questionnaireEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(CQuestionnaireEntity.class, dataGrid);

        cq.eq("deleted", 0);
        cq.eq("effective", 1);
        cq.addOrder("id", SortDirection.desc);

        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, questionnaireEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 新增或编辑页面跳转
     *
     * @param entity
     * @param req
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdate(CQuestionnaireEntity entity, HttpServletRequest req) {
        if (entity.getId() != null) {
            entity = commonService.getEntity(CQuestionnaireEntity.class, entity.getId());
        }
        req.setAttribute("entity", entity);
        return new ModelAndView("clound/quest/addorupdate");
    }

    /**
     * 明细查看
     *
     * @param entity
     * @param req
     * @return
     */
    @RequestMapping(params = "detail")
    public ModelAndView detail(CQuestionnaireEntity entity, HttpServletRequest req) {
        if (entity.getId() != null) {
            entity = commonService.getEntity(CQuestionnaireEntity.class, entity.getId());
        }
        req.setAttribute("entity", entity);
        return new ModelAndView("clound/quest/detail");
    }


    /**
     * 新增及修改的数据处理
     *
     * @param entity
     * @return
     */
    @RequestMapping(params = "saveorupdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveorupdate(CQuestionnaireEntity entity) {
        AjaxJson j = new AjaxJson();
        entity.setDeleted(0);
        entity.setSort(0);
        try {
            if (entity.getId() != null) {//修改操作
                CQuestionnaireEntity obj = commonService.getEntity(CQuestionnaireEntity.class, entity.getId());
                MyBeanUtils.copyBeanNotNull2Bean(entity, obj);
                commonService.saveOrUpdate(obj);
                j.setMsg("操作成功");
            } else {//保存操作
                commonService.save(entity);
                j.setMsg("操作成功");
            }
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }


    /**
     * 删除操作
     *
     * @param entity
     * @return
     */
    @RequestMapping(params = "del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson del(CQuestionnaireEntity entity, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            if (entity.getId() != null) {//修改
                entity = commonService.get(CQuestionnaireEntity.class, entity.getId());
                entity.setDeleted(1);
                commonService.saveOrUpdate(entity);
                j.setMsg("操作成功");
            } else {
                j.setMsg("量表不存在，请刷新后重试。");
            }
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }

    /**
     * 禁用和启用操作
     *
     * @param entity
     * @return
     */
    @RequestMapping(params = "stop_or_start", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson stop_or_start(CQuestionnaireEntity entity, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            if (entity.getId() != null) {//修改
                entity = commonService.get(CQuestionnaireEntity.class, entity.getId());
                if (entity.getEffective() == 0)
                    entity.setEffective(1);
                else
                    entity.setEffective(0);
                commonService.saveOrUpdate(entity);
                j.setMsg("操作成功");
            } else {
                j.setMsg("量表不存在，请刷新后重试。");
            }
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }


    /**
     * 模版导入页面跳转
     *
     * @return
     */
    @RequestMapping(params = "importTemplate")
    public ModelAndView importTemplate(CQuestionnaireEntity entity, HttpServletRequest request) {
        Integer qsId = entity.getId();
        request.setAttribute("qsId", qsId);
        return new ModelAndView("clound/quest/importTemplate");
    }


    /**
     * 模版导入过程进行
     *
     * @return
     */
    @Autowired
    private CQuestionnaireService cQuestionnaireService;

    @RequestMapping(params = "importing", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson importing(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            String ids = request.getParameter("ids");//选择拷贝的量表id集合
            cQuestionnaireService.importing(ids);
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }

        return j;
    }


    /**
     * 量表选择（单选） 页面跳转
     *
     * @return
     */
    @RequestMapping(params = "selectList")
    public ModelAndView selectList(CQuestionnaireEntity entity, HttpServletRequest request) {
        return new ModelAndView("clound/quest/selectList");
    }
}
